<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_RequestController extends Core_Controller_Action_Standard {
	protected $_navigation;
	
	public function init() {
		if (! $this->_helper->requireAuth ()->setAuthParams ( 'badge', null, 'request' )->isValid ())
			return;
		
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			if (0 !== ($request_id = ( int ) $this->_getParam ( 'request_id' )) && null !== ($request = Engine_Api::_ ()->getItem ( 'badge_request', $request_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $request );
			} else if (0 !== ($badge_id = ( int ) $this->_getParam ( 'badge_id' )) && null !== ($badge = Engine_Api::_ ()->getItem ( 'badge', $badge_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $badge );
			}
		}
		
		$this->_helper->requireUser->addActionRequires ( array ('browse', 'create', 'view' ) );
		
		$this->_helper->requireSubject->setActionRequireTypes ( array ('create' => 'badge', 'view' => 'badge_request' ) );
	}
	
	public function browseAction() {
		
		// Get navigation
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'badge_main' );
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->form = $form = new Badge_Form_Filter_Request ();
		
		$values = array ();
		// Populate form data
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
		}
		
		$values = Engine_Api::_ ()->getApi ( 'filter', 'radcodes' )->removeKeyEmptyValues ( $values );
		$this->view->formValues = $values;
		
		$this->view->assign ( $values );
		
		$values ['user'] = $viewer;
		$values ['limit'] = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'badge.requestperpage', 10 );
		
		$this->view->paginator = $paginator = Engine_Api::_ ()->badge ()->getRequestsPaginator ( $values );
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
	}
	
	public function createAction() {
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->badge = $badge = Engine_Api::_ ()->core ()->getSubject ( 'badge' );
		
		$this->view->form = $form = new Badge_Form_Request ();
		
		if (! $badge->award_member_request) {
			$this->view->error = 'Badge request is disabled.';
		} else if ($badge->award_member_request == 1 && ! $badge->testMemberRequirements ( $viewer )) {
			$this->view->error = 'You do not meet member requirements to request for this badge.';
		}
		
		if ($this->view->error) {
			return;
		}
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		$table = Engine_Api::_ ()->getItemTable ( 'badge_request' );
		$db = $table->getAdapter ();
		$db->beginTransaction ();
		
		try {
			
			$request = $table->createRow ();
			$request->setFromArray ( array_merge ( $form->getValues (), array ('status' => 'pending', 'read' => 0, 'badge_id' => $badge->getIdentity (), 'user_id' => $viewer->getIdentity () ) ) );
			$request->save ();
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		// Close smoothbox
		$currentContext = $this->_helper->contextSwitch->getCurrentContext ();
		if (null === $currentContext) {
			return $this->_helper->redirector->gotoUrl ( $badge->getHref () );
		} else if ('smoothbox' === $currentContext) {
			return $this->_forward ( 'success', 'utility', 'core', array ('messages' => $this->view->translate ( 'Your request has been submitted.' ), 'smoothboxClose' => true, 'parentRefresh' => false ) );
		}
	
	}
	
	public function viewAction() {
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->badge_request = $badge_request = Engine_Api::_ ()->core ()->getSubject ( 'badge_request' );
		
		if ($viewer->getIdentity () != $badge_request->user_id) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
	
	}

}

